package com.jz.community.moduleshopping.address.smarttop.manager;

import android.app.Activity;
import android.content.Context;

import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.moduleshopping.address.bean.RegionsInfo;
import com.jz.community.moduleshopping.address.smarttop.bean.City;
import com.jz.community.moduleshopping.address.smarttop.bean.County;
import com.jz.community.moduleshopping.address.smarttop.bean.Province;
import com.jz.community.moduleshopping.address.smarttop.bean.Street;
import com.jz.community.moduleshopping.address.task.GetRegionsTask;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by huanghaojie on 2016/10/19.
 * 对地址库的增删改查
 */

public class AddressDictManager {
    private Context context;
    private RegionsInfo regionsInfo;
    private List<Province> provinceList;
    private List<City> cityList;
    private List<Street> streetList;
    private List<County> countyList;

    private RegionsProvinceListener regionsProvinceListener;

    private RegionsCityListener regionsCityListener;

    private RegionsStreetListener regionsStreetListener;

    private RegionsCountyListener regionsCountyListener;

    public void setRegionsCityListener(RegionsCityListener regionsCityListener) {
        this.regionsCityListener = regionsCityListener;
    }

    public void setRegionsStreetListener(RegionsStreetListener regionsStreetListener) {
        this.regionsStreetListener = regionsStreetListener;
    }

    public void setRegionsCountyListener(RegionsCountyListener regionsCountyListener) {
        this.regionsCountyListener = regionsCountyListener;
    }

    public void setRegionsProvinceListener(RegionsProvinceListener regionsProvinceListener) {
        this.regionsProvinceListener = regionsProvinceListener;
    }

    public interface RegionsProvinceListener {
        void setProvinceList(List<Province> provinceList);
    }

    public interface RegionsCityListener {

        void setCityList(List<City> cityList);

    }

    public interface RegionsStreetListener {

        void setStreetList(List<Street> streetList);

    }

    public interface RegionsCountyListener {

        void setCountyList(List<County> countyList);
    }

    public AddressDictManager(Context context) {
        this.context = context;
    }

    /**
     * 获取省份列表
     *
     * @return
     */
    public void loadProvinceList() {
        provinceList = new ArrayList<>();
        new GetRegionsTask((Activity) context, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                RegionsInfo regionsInfo = (RegionsInfo) o;
                if (!Preconditions.isNullOrEmpty(regionsInfo)) {
                    List<RegionsInfo.EmbeddedBean.ContentBean> regionsList = regionsInfo.get_embedded().getContent();
                    for (RegionsInfo.EmbeddedBean.ContentBean regionsItem : regionsList) {
                        Province province = new Province();
                        province.id = regionsItem.getId();
                        province.name = regionsItem.getName();
                        provinceList.add(province);
                    }
                    regionsProvinceListener.setProvinceList(provinceList);
                }
            }
        }).execute("0", "1");
    }

    /**
     * 获取省份
     *
     * @param provinceCode
     * @return
     */
    public Province getProvinceBean(String provinceCode) {
        for (Province regionsItem : provinceList) {
            if (regionsItem.id.equals(provinceCode)) {
                Province province = new Province();
                province.id = regionsItem.id;
                province.name = regionsItem.name;
                return province;
            }
        }
        return null;
    }

    /**
     * 获取省份对应的城市列表
     *
     * @return
     */
    public void getCityList(String provinceId) {
        cityList = new ArrayList<>();
        new GetRegionsTask((Activity) context, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                regionsInfo = (RegionsInfo) o;
                if (!Preconditions.isNullOrEmpty(regionsInfo)) {
                    List<RegionsInfo.EmbeddedBean.ContentBean> regionsList = regionsInfo.get_embedded().getContent();
                    for (RegionsInfo.EmbeddedBean.ContentBean regionsItem : regionsList) {
                        City city = new City();
                        city.id = regionsItem.getId();
                        city.name = regionsItem.getName();
                        cityList.add(city);
                    }
                    regionsCityListener.setCityList(cityList);
                }
            }
        }).execute(provinceId, "1");
    }

    /**
     * 获取城市
     *
     * @return
     */
    public City getCityBean(String cityCode) {
        for (City cityItem : cityList) {
            if (cityItem.id.equals(cityCode)) {
                City city = new City();
                city.id = cityItem.id;
                city.name = cityItem.name;
                cityList.add(city);
                return city;
            }
        }
        return null;
    }

    /**
     * 获取城市对应的区，乡镇列表
     *
     * @return
     */
    public void getCountyList(String cityId) {
        countyList = new ArrayList<>();
        new GetRegionsTask((Activity) context, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                regionsInfo = (RegionsInfo) o;
                if (!Preconditions.isNullOrEmpty(regionsInfo)) {
                    List<RegionsInfo.EmbeddedBean.ContentBean> regionsList = regionsInfo.get_embedded().getContent();
                    for (RegionsInfo.EmbeddedBean.ContentBean regionsItem : regionsList) {
                        County county = new County();
                        county.id = regionsItem.getId();
                        county.name = regionsItem.getName();
                        countyList.add(county);
                    }
                    regionsCountyListener.setCountyList(countyList);
                }
            }
        }).execute(cityId, "1");
    }

    public County getCountyBean(String countyCode) {
        for (County countyItem : countyList) {
            if (countyItem.id.equals(countyCode)) {
                County county = new County();
                county.id = countyItem.id;
                county.name = countyItem.name;
                countyList.add(county);
                return county;
            }
        }
        return null;
    }

    /**
     * 获取区，乡镇对应的街道列表
     *
     * @return
     */
    public void getStreetList(String countyId) {
        streetList = new ArrayList<>();
        new GetRegionsTask((Activity) context, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                regionsInfo = (RegionsInfo) o;
                if (!Preconditions.isNullOrEmpty(regionsInfo)) {
                    List<RegionsInfo.EmbeddedBean.ContentBean> regionsList = regionsInfo.get_embedded().getContent();
                    for (RegionsInfo.EmbeddedBean.ContentBean regionsItem : regionsList) {
                        Street street = new Street();
                        street.id = regionsItem.getId();
                        street.name = regionsItem.getName();
                        streetList.add(street);
                    }
                    regionsStreetListener.setStreetList(streetList);
                }
            }
        }).execute(countyId, "1");
    }

    public Street getStreetBean(String streetCode) {
        for (Street streetItem : streetList) {
            if (streetItem.id.equals(streetCode)) {
                Street street = new Street();
                street.id = streetItem.id;
                street.name = streetItem.name;
                streetList.add(street);
                return street;
            }
        }
        return null;
    }
}
